package com.waves.chat.listener;

import cn.hutool.core.collection.CollUtil;
import com.rabbitmq.client.Channel;
import com.waves.chat.cache.GroupMemberCache;
import com.waves.chat.cache.HotRoomCache;
import com.waves.chat.cache.RoomCache;
import com.waves.chat.dao.ContactDao;
import com.waves.chat.dao.MessageDao;
import com.waves.chat.dao.RoomDao;
import com.waves.chat.dao.RoomFriendDao;
import com.waves.chat.domain.entity.Contact;
import com.waves.chat.domain.entity.Room;
import com.waves.chat.domain.entity.RoomFriend;
import com.waves.chat.domain.enums.RoomTypeEnum;
import com.waves.chat.domain.vo.ChatMessageVO;
import com.waves.chat.service.ChatService;
import com.waves.chat.service.WeChatMsgOperationService;
import com.waves.common.domain.dto.MsgSendMessageDTO;
import com.waves.core.constant.MQConstant;
import com.waves.user.build.WSBuilder;
import com.waves.user.cache.UserCache;
import com.waves.user.service.PushService;
import com.waves.websocket.service.WebSocketService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import javax.annotation.Resource;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

/**
 * @author dlw
 * @email
 */
@Service
public class ChatListener {

    @Resource
    private WebSocketService webSocketService;
    @Resource
    private ChatService chatService;
    @Resource
    private MessageDao messageDao;
    //    @Resource
//    private IChatAIService openAIService;
    @Resource
    WeChatMsgOperationService weChatMsgOperationService;
    @Resource
    private RoomCache roomCache;
    @Resource
    private RoomDao roomDao;
    @Resource
    private GroupMemberCache groupMemberCache;
    @Resource
    private UserCache userCache;
    @Resource
    private RoomFriendDao roomFriendDao;
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;
    @Resource
    private ContactDao contactDao;
    @Resource
    private HotRoomCache hotRoomCache;
    @Resource
    private PushService pushService;

    @RabbitListener(queues = MQConstant.CHAT_SEND_MSG_QUEUE)
    public void listener(MsgSendMessageDTO dto, Channel channel, Message msg) throws IOException {
        com.waves.chat.domain.entity.Message message = messageDao.getById(dto.getMsgId());
        Room room = roomCache.get(message.getRoomId());
        ChatMessageVO msgResp = chatService.getMsgResp(message, null);
        //所有房间更新房间最新消息
        roomDao.refreshActiveTime(room.getId(), message.getId(), new Date(message.getCreatedTime()));
        roomCache.delete(room.getId());
        if (room.isHotRoom()) {//热门群聊推送所有在线的人
            //更新热门群聊时间-redis
            hotRoomCache.refreshActiveTime(room.getId(), new Date(message.getCreatedTime()));
            //推送所有人
            pushService.sendPushMsg(WSBuilder.buildMsgSend(msgResp));
        } else {
            List<Long> memberUidList = new ArrayList<>();
            if (Objects.equals(room.getType(), RoomTypeEnum.GROUP.getType())) {//普通群聊推送所有群成员
                memberUidList = groupMemberCache.getMemberUidList(room.getId());
            } else if (Objects.equals(room.getType(), RoomTypeEnum.FRIEND.getType())) {//单聊对象
                //对单人推送
                RoomFriend roomFriend = roomFriendDao.getByRoomId(room.getId());
                memberUidList = Arrays.asList(roomFriend.getUid1(), roomFriend.getUid2());
            }

            //更新所有群成员的会话时间
//            for (Long uid : memberUidList) {
//                // 如果会话存在，并且会话更新时间为空
//                Contact contact = contactDao.get(uid, room.getId());
//                if (contact != null && contact.getActiveTime() == null) {
//                    memberUidList.remove(uid);
//                }
//            }
            if (CollUtil.isNotEmpty(memberUidList)) {
                contactDao.refreshOrCreateActiveTime(room.getId(), memberUidList, message.getId(), new Date(message.getCreatedTime()));
            }
            //推送房间成员
            pushService.sendPushMsg(WSBuilder.buildMsgSend(msgResp), memberUidList);
        }

    }


}
